<template>
    <div>
        <my-nvaigation-bar></my-nvaigation-bar>
        <div id="Glxt_containner">
            <el-row>
                <el-col :span="24" id="Glxt_bigtitle">帖子管理postInfo</el-col>
            </el-row>
            <!--查询表单-->
            <el-card shadow="never">
            <el-form :inline="true" class="demo-form-inline">
            <el-form-item label="标题">
                <el-input v-model="searchObj.title" placeholder="帖子的标题" /> 
            </el-form-item>
            <el-form-item label="内容">
                <el-input v-model="searchObj.text" placeholder="帖子的内容" /> 
            </el-form-item>
            <el-form-item label="类别">
                <el-select v-model="searchObj.type" placeholder="请选择类型">
                    <el-option
                    v-for="type in typeList"
                    :key="type.id"
                    
                    :label="type.typeName"
                    :value="type.id"
                    ></el-option>
                </el-select>
            </el-form-item>
            <!--这个单选框↑，使用完v-for要空一行，要不的话会有红色的>，然后下面的代码都加粗了，不知道为什么-->

            <el-form-item label="作者id">
                <el-input v-model="searchObj.userId" placeholder="作者的id" /> 
            </el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="fetchData()">查询</el-button>
            <el-button type="default" @click="resetData()">清空</el-button>
            </el-form>
            </el-card>
            
            <!--表格-->
            <el-table :data="list" style="width: 100%">
                <el-table-column prop="id" label="ID"></el-table-column>
                <el-table-column prop="title" label="标题"></el-table-column>
                <el-table-column prop="text" label="内容"></el-table-column>
                <el-table-column prop="pic" label="图片">
                    <template slot-scope="scope">
                        <img :src="scope.row.pic" style="width: 44px; height: 33px;"/>
                    </template> 
                </el-table-column>
                <el-table-column label="类别">
                    <template slot-scope="scope">
                        <el-tag>{{getTypeById(scope.row.type)}}</el-tag>
                    </template>
                </el-table-column>
                <el-table-column prop="userId" label="作者id"></el-table-column>
                <el-table-column prop="createTime" label="创建时间"></el-table-column>
                <el-table-column prop="updateTime" label="修改时间"></el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-button @click="editClick(scope.row.id)" type="text" size="small">编辑</el-button>
                        <el-button type="text" size="small" @click="deleteClick(scope.row.id)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <!--页码-->
            <el-row>
                <el-col :span="24"><el-pagination background layout="prev, pager, next" :total="total" :current-page="page" @current-change="changeCurrentPage"></el-pagination></el-col>
            </el-row>

            <!--编辑-->
            <el-drawer
            title="编辑帖子信息"
            :visible.sync="drawer"
            with-header=true
            :before-close="handleClose">
            <div style="padding:30px">
                <el-form ref="editUser" :model="form" label-width="60px">
                    <el-form-item label="ID">
                        <el-input v-model="editPost.id" disabled></el-input>
                    </el-form-item>
                    <el-form-item label="标题">
                        <el-input v-model="editPost.title"></el-input>
                    </el-form-item>
                    <el-form-item label="内容">
                        <el-input v-model="editPost.text"></el-input>
                    </el-form-item>
                    <el-form-item label="图片">
                        <el-upload class="avatar-uploader"
                                action= "http://localhost:8333/post/postInfo/upload"
                                :show-file-list="false"
                                :on-success="handleSuccess"
                                ref="upload">
                            <img v-if="img" :src="img" class="image"/>
                            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                        </el-upload>
                    </el-form-item>
                    <el-form-item label="类别">
                        <el-select v-model="editPost.type" placeholder="请选择类型">
                            <el-option
                            v-for="type in typeList"
                            :key="type.id"

                            :label="type.typeName"
                            :value="type.id">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="作者Id">
                        <el-input v-model="editPost.userId" disabled></el-input>
                    </el-form-item>
                    <div style="margin-top: 10px;">
                        <el-button type="primary" @click="onSubmit">完成</el-button>
                    </div>                    
                </el-form>
            </div>
            </el-drawer>
        </div>
    </div>
</template>

<script>
import MyNvaigationBar from '../../components/MyNvaigationBar.vue';
import PostTypeApi from '../../api/post/postType'
import PostInfoApi from '../../api/post/postInfo'

export default {
components: { MyNvaigationBar },
data() {
    return {
        list: [], // postInfo列表
        total: 0, // 总记录数
        page: 1, // 页码
        limit: 10, // 每页记录数
        searchObj: {}, // 查询条件
        typeList: [],// 分类列表
        drawer: false,
        editPost: {}, //修改帖子信息的内容
        img: ''
    }
},
methods: {
    handleSuccess (response, file, fileList) {
            console.log(response)
              this.editPost.pic = `http://localhost:8333/post/postInfo/download?name=${response.data}`
              this.img = this.saveObj.pic
              console.log(this.saveObj.pic)
          },
     //修改帖子信息
     onSubmit() {
            this.$confirm('你确定要更改帖子信息吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                return PostInfoApi.updatePost(this.editPost)
            }).then((res) => {
                if (res.data.code == 200) {
                    this.$message.success(res.data.message)
                    this.fetchData()
                    this.drawer = false
                } else {
                    this.$message.error(res.data.message);
                }
            }).catch(err => {
                if (err === 'cancel') {
                    this.$message.info('取消编辑')
                }
            })
        },
    //删除帖子
    deleteClick(id) {
            this.$confirm('你确定要删除'+id+'号帖子吗','提示',{
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                return PostInfoApi.remove(id)
            }).then((res) => {
                this.fetchData()
                this.$message.info(res.data.message)
            }).catch(err => {
                if (err === 'cancel') {
                    this.$message.info('取消删除')
                }
            })  
        },
    // 改变页码，page：回调参数，表示当前选中的“页码”
    changeCurrentPage(page) {
            this.page = page
            this.fetchData()
        },
    //编辑信息
    editClick(id) {
            this.drawer = true;
            PostInfoApi.getPostById(id)
            .then(res => {
                this.editPost = res.data.data
                this.img = this.editPost.pic
                console.log(this.editPost)
            })
        },
    // 重置表单
    resetData() {
            this.searchObj = {}
            this.fetchData()
        },
    //编辑信息关闭提醒
    handleClose(done) {
        this.$confirm('确定退出编辑帖子信息？')
          .then(_ => {
            done();
          })
          .catch(_ => {});
        },
    //获取所有分类
    getAllType() {
        PostTypeApi.getAllType()
        .then(res => {
            this.typeList = res.data.data
            console.log(this.typeList)
        })
    },
    //数据初始化
    fetchData() {
        PostInfoApi.pageList(this.page, this.limit, this.searchObj)
        .then(res => {
            this.list = res.data.data.records
            this.total = res.data.data.total
        })
    },
},
computed: {
    //跟据id获取type
    getTypeById() {
        return id => {
            const item = this.typeList.find(item => item.id === id)
            return item ? item.typeName : ''
        }
    },
},
created() {
    this.getAllType(),
    this.fetchData()
}
}

</script>

<style>
    #Glxt_containner{
        width: auto;
        height: auto;
        border:3px dashed #42b983;
        padding-top: 8px;
        padding-bottom: 8px;
        border-radius: 8px;
        margin-top: 10px;
    }

    #Glxt_bigtitle {
    font-family: Avenir, Helvetica, Arial, sans-serif;
    font-size: 20px;
    font-weight: bolder;
    color:#42b983;
    }

    .image{
    width: 100%;
    height: 324px;
}

</style>